package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000117610570";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDsodyWI99jWyjmE5HjdNhkm5OEyzffviZYM00YgspsTTE+yVT9G2ON6ikG7kUQoRNY7MS+kknArS1XsrLL+8WO1lKt4yPABjGpx7XlpfAplnX+AB2h81zL+OWGmwun1hGVNw8Bwwuyu/6Ygj+vAkPTfxByUxIK9l54R1n7FP1oyUDfmkUH9T1n/tu4XfatHdxUPMtfJn+yZDN3qS8hpV13vrIAlVJZgaoosiAWQjdWXfkPYecBRXjyPgqofMiUVeLGcaSUEVxTWmdCqtYBKxcrFhXGf8r4ZJZoEn4gff6GbwpRNyySjPeLOuNdWd2R2mQCazgxZyyQi1gmWZMZGHY7AgMBAAECggEBAOQIGx1oVlZVu7Tz+OubZwiUz/TxLNflUug+D7ZmG5GAVctukwMSSL6yf3TZWh7rSkywZexL5tzRRde0Mg7Wggvix2wgbgwXK/azqLs0TJqXDv+FtwihD7TUFsO/V90WMn17wG9vOS+1cGQhaCNLaHdAhGXXXZTI1ELOZ4zJLmaxZ8fSCXEl0IrstoG0cgGN6wNSFQ3/4KkJzqiggf/2fFLc2UVX4Z6e7ynGqD+NhSrPwNf6954LW4tQKbWLU3bFNq/y+Bqk/bpxEYlxUhuJAM7JSY8NJLRpvoiWONQsVfc2G4dzlInw0PyOVwCuJHMSFDD+hu2AERYXCO7Ck7mOxyECgYEA/bZacsDC/XUICiHXNCfEJI+X+sQ2Z1uFIuuoWeIIHFOSjKY1KxHgDCYjjmYNkbtOSvWnuUcyYnsjsS/9Gh70rmLUo2VpEUDxouQjd36Q+nzV71vlQ3VT5bAkVcs20ISwZZOL5/a8wf72F6KfsTpbwRN858BxW1bYWA1ENXMkE3ECgYEA7sQVEmmCHyD4dWjGR5gIj16rRk+ckdPkY1zR0mF4qo5CrMGJe7d1IoEmJ4aZ7NpiudYl01RLe2MVct3/rOS6JSWwZ5BFEdey6w3Q0RRUYctE87vgvchD2qJzolrHK0S6kyiUhADqLSKnKQmLFDK6JBxOoVVKegmgx5vlhSlStmsCgYEAwJWyFJczrBcw8hf79lAVODdzgtt7/HrhFb/SO+AyjeX2QSNNCpN4AWDV7pqSxENsy8Oq/B4KJe0ZC7/EnRZQXysQA7PPzFQC18XbEjKVtD4cWqPj5I/kDl7lReaKhj+icohMU9jcMVnqzcgY9fu5eFblojlXEqFaeYQgeX4cLVECgYAHC8ZLas2qOYDbhiIDztrB0FthkcvfCc+9lumiOboj3lrdWkDCY+GTCRilJfW45aDwNv+awuwPUhc58o8u/Pj5S6lvBPTk1+axxRCaeu0ajbMVNkAlmZlXBGBBNEiYYEjZOuNXGefPOSyRq3RtPin8EtHHNQsjmtEh0tntGhEUzQKBgGVtO3iiPmjNnHXr2rPcMEKnu37yBPWvvmJkEnezs4UwACh4UB21P/ZUpPa/yKfEs4XfkSCiqw5R5yABXmvIqcIohAfjt4EOj98df/r9oPEgL+ntdN6z2ek9V4GBz043ZJeZVAlgxBRhpiDusN+24m9vpBxj+c5XlZD+7TgJ4T7F";
	// 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuQv9yc2AoUcgzCdzaSueOHyLKI+Dr+UAp9I0KShGfh8+3Zg1hvcVjhoSv0H7Vnr41VSRjxksagnLN2KGUihlBF/9d+VeWIAEZlWBtTdrqhFeV2Jznc3zofNYyDvpUhRc7IrwAMZW8JvzHkNiFdsrlgSmb4hF0S81WqhdVIwkQnIZsExXgE1ZoZYE1WLpuHsp7MlcbsCVOeUE+66XMrHLlPA4LvRJ6eeKL+FkyGjcgOYyGaWcaOCIFLPjHiZ34ARS80s8oRFe3hAq4vpCFh5UVaBd1iJJSqOtalqUbBitLwbLyWsHre5dihwIBJtSeB/n76ruO9MLjqTMmudWs2PxrQIDAQAB";
	// 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://j9zoofqhm2.52http.net/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 自动关单时间
    private String timeout = "15m";

    // 支付宝网关；https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        // 30分钟内不付款就会自动关单
        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\"" + timeout + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝响应了：" + result);
        return result;
    }
}
